<%@page import="java.text.NumberFormat"%><% // Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License. %><%@page language="java" contentType="text/plain; charset=UTF-8" %><%@page pageEncoding="UTF-8" %><%@page import="edu.cmu.lti.sepia.util.ServletUtil"%><%@page import="edu.cmu.lti.sepia.data.session.SessionManager"%><%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%><%@page import="edu.cmu.lti.sepia.data.db.*"%><%@page import="edu.cmu.lti.sepia.util.Logger"%><%@page import="edu.cmu.lti.sepia.infra.topic.model.Topic"%><%@page import="edu.cmu.lti.sepia.infra.topic.model.Nugget"%><%@page import="java.util.List"%><%@page import="edu.cmu.lti.sepia.servlet.Commons"%><%
request.setCharacterEncoding("UTF8");
response.setContentType("text/plain; charset=utf-8");
response.setHeader("progma", "no-cache");
response.setHeader("Cache-Control", "no-cache");

SQL sql = new SQL();
ServletUtil s = new ServletUtil( request );
String taskNameShort;
if ( request.getParameter("taskNameShort")!=null ) {
	taskNameShort = (String)request.getParameter("taskNameShort");
	SessionManager.storeTask(sql, taskNameShort, session);
} else {
	taskNameShort = (String)session.getAttribute("taskNameShort");
}
String type = (String)request.getParameter("type");
String trgLang = s.readString("trgLang","");
int userId = s.readInt("userId", -1);

response.setHeader("Content-Disposition","attachment; filename=\""+taskNameShort+"-nuggets.txt"+"\"");

List<Topic> topics = TopicDAO.getGoldStandard( sql, taskNameShort, userId, null );

Logger.log( sql, session, "EXPORT Nuggets (TEXT)", "" );
sql.close();
TopicDAO.removeSimilarNuggets(topics);
TopicDAO.generateExternalIds(topics,trgLang,4);
TopicDAO.removeUnselected(topics);

//Exactly same as gs xml export up to this point
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(4);
nf.setMaximumFractionDigits(4);

for (Topic t : topics) {
  StringBuilder sb = new StringBuilder();
  String tid = t.getExternalId();
  sb.append("Topic id "+tid+": ");
  sb.append( t.getQuestionSrc() + " / " + t.getQuestionTrg()+"\n" );// TODO: monolingal?
  for ( int i=0; i<t.getNuggets().size(); i++ ) {
    Nugget n = t.getNuggets().get(i);
    sb.append(tid+"\t"+(i+1)+"\t"+nf.format(n.getWeight())+"\t"+n.getText()+"\n");
  }
  sb.append("\n\n");
  out.print(sb.toString());
}
%>